package com.leve.admin.controller;

import com.leve.admin.domain.PushMessage;
import com.leve.admin.domain.PushType;
import com.leve.admin.domain.ResultBase;
import com.leve.admin.service.PushService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;

@RestController
@RequestMapping("/push")
@Slf4j
public class PushController {

    @Autowired
    private PushService pushService;

    @ApiOperation("添加系统推送")
    @PostMapping(value = "sendMsg", produces = APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBase addPush(
            HttpServletRequest request) {
        String groupid = request.getParameter("groupid");
        String group_name = request.getParameter("group_name");
        String context = request.getParameter("context");
        String push_date = request.getParameter("push_date");
        String push_type = request.getParameter("push_type");
        PushMessage pushMessage = new PushMessage();
        pushMessage.setGroupid(groupid);
        pushMessage.setGroupName(group_name);
        pushMessage.setContext(context);
        pushMessage.setImgUrl("");
        pushMessage.setPushDate(push_date);
        if ("IM_PUSH".equals(push_type)) {
            pushMessage.setPushType(PushType.IM_PUSH);
        } else if ("SYS_PUSH".equals(push_type)) {
            pushMessage.setPushType(PushType.SYS_PUSH);
        } else {
            pushMessage.setPushType(PushType.SMS_PUSH);
        }
        return pushService.addPush(pushMessage);
    }


    @ApiOperation("任务列表")
    @GetMapping(value = "pushs", produces = APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResultBase pushs(@PageableDefault(size = 20) Pageable pageable) {
        PageRequest pageRequest = new PageRequest(pageable.getPageNumber() - 1, pageable.getPageSize());
        return pushService.pushs(pageRequest);
    }
}
